import tensorflow as tf
import numpy as np

# ---------------
# g = tf.compat.v1.Graph()
# with g.as_default():
#     a = tf.compat.v1.placeholder(name='a', shape=[], dtype=tf.int64)
#     b = tf.compat.v1.placeholder(name='b', shape=[], dtype=tf.int64)
#     c = tf.add(a, b, name='c')
#     d = tf.compat.v1.placeholder(name='d', shape=[], dtype=tf.int64)
#     e = tf.multiply(c, d, name='e')
#
# with tf.compat.v1.Session(graph = g) as sess:
#     # fetches的结果非常像一个函数的返回值，而feed_dict中的占位符相当于函数的参数序列。
#     result = sess.run(fetches = e,feed_dict = {a:1, b:2, d:3})
#     print(result)

# ---------------
# # 0阶张量
# x0 = tf.constant(1)
# # 1阶张量
# x1 = tf.constant([1, 2])
# # 2阶张量
# x2 = tf.constant([[1, 2], [3, 4]])
# # 3阶张量
# x3 = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
#
# print(x1)
# print(x2)
# print(x3)

# ---------------
# # 使用  tf.constant 创建指定元素内容的张量
# a1 = tf.constant([[1, 2, 3, 4]])
# # 使用 tf.ones 创建元素内容全为 1 的张量
# a2 = tf.ones((1,4))
# # 使用 tf.zeros 创建元素内容全为 0 的张量
# a3 = tf.zeros((1,4))
# # 使用 tf.range 创建元素变化有规律的的张量(以1开始，间隔2，最大到8)
# a4 = tf.range(start=1, limit=8, delta=2)
#
# print(a1)
# print(a2)
# print(a3)
# print(a4)


# ---------------
# # 3阶张量
# x3 = tf.constant([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
# # 将张量传入 tf.size函数 获得 size
# x3_size = tf.size(x3)
# print('x3_size = ', x3_size.numpy())

# ---------------
# 2阶张量
# t1 = tf.constant([[1, 2], [3, 4]])
# # 指定数据类型为 float 的 2阶张量
# t2 = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
# print('t1 = ', t1)
# print('t2 = ', t2)

# t1 = tf.constant([0,1,2,3,4,5,6,7,8,9])
# print('first =', t1[0].numpy())
# print('third to last =', t1[-3].numpy())
# print('2 to -3 =', t1[2:-3].numpy())
# print('2 to -3 with step 2 =', t1[2:-3:2].numpy())

# ---------------
# t3 = tf.constant([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
# print('t3[0] = ', t3[1].numpy())
# print('t3[0,1] = ', t3[1,0].numpy())
# print('t3[0,1,2] = ', t3[1,0,2].numpy())

# ----------------
# t1 = tf.constant([[1,2,3],[4,5,6]])
# print('t1 : ', t1.shape)
# print(t1.numpy())
# t2 = tf.reshape(t1, [3,2])
# print('t2 : ',t2.shape)
# print(t2.numpy())

# ----------------
# t1 = tf.constant([[1, 2], [3, 4]])
# print('t1 = ', t1)
# t2 = tf.expand_dims(t1, axis=0)
# print('t2 = ', t2)
# t1 = tf.constant([[1.,2.],[8.,9.]])
# t2 = tf.constant([[3.,4.],[7.,8.]])
# # 取函数值开方
# t3 = tf.sqrt(t1)
# print("t3 = ", t3)
# # 通过运算符重载，判断每一位是否大于2，大于的取True，否则取Flase
# t4 = (t1 > 2)
# print("t4 = ", t4)
# # 取两个张量中，对应位置较小的数作为参赛
# t5 = tf.minimum(t1,t2)
# print("t5 = ", t5)

# ---------------
# t1 = tf.constant([[1., 2., 3.],
#                   [4., 5., 6.]])
# # print(tf.reduce_max(t1))
# # print(tf.reduce_max(t1,keepdims=True))
# # print(tf.reduce_max(t1,0))
# # print(tf.reduce_max(t1,1))
#
# print(tf.reduce_mean(t1))
# print(tf.reduce_mean(t1,0))
# print(tf.reduce_mean(t1,1))

# ---------------
# t1 = tf.constant([[True, False, False],
#                   [True, True,  True]])
# print(tf.reduce_any(t1))
# print(tf.reduce_any(t1,0))
# print(tf.reduce_any(t1,1))

# ---------------
# t1 = tf.constant([[1,2],[3,4]])
# t2 = tf.constant([[5,6],[7,8]])
# print(tf.matmul(t1,t2))
# print(t1@t2)
# print(tf.transpose(t1))

# ----------------
# t1 = tf.ones((3,2,1))
# print('t1 = ', t1)
# t2 = tf.ones((1,2))
# print('t2 = ', t2)
# print('t1 + t2 = ', (t1+t2))

# #----------------
# # 使用整型初始化变量
# var1 = tf.Variable(1)
# # 使用浮点型初始化变量
# var2 = tf.Variable(2.)
# # 使用列表初始化变量
# var3 = tf.Variable([1,2,3])
# # 使用张量初始化变量
# var4 = tf.Variable(tf.constant([[1, 2], [3, 4]]))
# print('var1 = ', var1)
# print('var2 = ', var2)
# print('var3 = ', var3)
# print('var4 = ', var4)

# ----------------
# var2 = tf.Variable(tf.constant([[1, 2], [3, 4]]), name="var2")
# print('var2.name = ', var2.name)

# ----------------
var1 = tf.Variable(tf.constant([[0, 0], [0, 0]]))
var1.assign(([1, 2], [3, 4]))
print('setp1 = ', var1)
var1.assign_add(([1, 1], [1, 1]))
print('setp2 = ', var1)
